package com.cn.zengzeng.spbzengzeng.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.text.StringEscapeUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * @Author: zhangzengzeng
 * @Date: 2021/9/30 14:41
 */
public class CheckUtils {
    private static final Pattern PATTERN = Pattern.compile("\\s*|\t|\r|\n|");
    private static final Pattern COMPILE = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
    public static boolean  checkEmail(String email){
        Matcher matcher = COMPILE.matcher(email);
        return matcher.matches();
    }
    public static boolean  checkPhone(String mobile){
        String str = "^[1][3-9]\\d{9}$";
        Pattern compile = Pattern.compile(str);
        Matcher matcher = compile.matcher(mobile);
        return matcher.matches();
    }
    public static JSONObject getJson(String json){
        String s = StringEscapeUtils.unescapeJava(json);
        String replace = s.replace(" ","");
        if (!replace.isEmpty()) {
            Matcher m = PATTERN.matcher(replace);
            replace = m.replaceAll("");
        }
        char[] chars = replace.toCharArray();
        boolean a = false;
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i < chars.length-1;i++) {
            if(a){
                a=false;
                continue;
            }
            //保留空字符串值
            if(chars[i]==':' && chars[i+1]=='"' && chars[i+2]=='"' && chars[i+3]==','){
                sb.append(chars[i]).append(chars[i+1]).append(chars[i+2]).append(chars[i+3]);
                i+=3;
                continue;
            }
            if(chars[i]==chars[i+1] && '"'==chars[i] ){
                sb.append(chars[i]);
                a=true;
            }else {
                sb.append(chars[i]);
            }
        }
        sb.append(chars[chars.length-1]);
        return JSON.parseObject(sb.toString());
    }
}
